﻿using ChongJu.ModelManager.Calibration;
using ChongJu.ModelManager.Camera;
using Public.Logger;
using Public.Tools;
using System;
using System.IO;

namespace ChongJu.GlobalInfo
{
    public class CameraConfig
    {

        #region Camera
        public static CameraModel CameraCfg
        {
            get
            {
                if(_cameraCfg == null)
                {
                    if (File.Exists(ConfigFileInfo.CamereConfig) == false)
                    {
                        var cfg = new CameraModel();
                        var content = Tool.ObjectToJson(cfg);
                        if (Tool.Wirte(ConfigFileInfo.CamereConfig, content))
                        {
                            _cameraCfg = cfg;
                            Logger.Debug("相机配置文件不存在，创建默认配置文件");
                        }
                        else
                            Logger.Error("相机配置文件不存在，创建默认配置文件异常");
                    }
                    else
                    {
                        var content = Tool.Read(ConfigFileInfo.CamereConfig);
                        _cameraCfg = Tool.JsonToObject<CameraModel>(content);
                        //相机参数中width必须为8的倍数，如果不是则需要变为倍数然后存入配置文件
                        int tempWidth = GetMultipleValue(_cameraCfg.Width, 8);
                        //相机参数中Height必须为4的倍数，如果不是则需要变为倍数然后存入配置文件
                        int tempHeight = GetMultipleValue(_cameraCfg.Height, 4);
                        if (tempWidth != _cameraCfg.Width || tempHeight != _cameraCfg.Height)
                        { 
                            UpdateCameraCfg(_cameraCfg);
                        }
                    }
                }

                return _cameraCfg;
            }
        }
        private static CameraModel _cameraCfg;

        public static int GetMultipleValue(int num, int multiple)
        {
            if (0 == num || 0 == multiple || num % multiple == 0)
            {
                return num;
            }
            else
            {
                int mod = num % multiple;
                if (mod > multiple / 2)
                    return num - mod + multiple;
                else
                    return num - mod;
            }
        }
        public static bool UpdateCameraCfg(CameraModel cfg = null)
        {
            if (null == cfg) 
            {
                cfg = CameraCfg;
            }

            try
            {
                cfg.Width = GetMultipleValue(cfg.Width, 8);
                cfg.Height = GetMultipleValue(cfg.Height, 4);
                var content = Tool.ObjectToJson(cfg);
                var bol = Tool.Wirte(ConfigFileInfo.CamereConfig, content);
                if (bol == true)
                    _cameraCfg = cfg;

                return bol;
            }
            catch(Exception ex)
            {
                Logger.Error("更新相机配置文件异常", ex);
                return false;
            }
        }

        #endregion


        #region Calibration
        public static CalibrationCfgModel CalibrationCfg
        {
            get
            {
                if (_calibrationCfg == null)
                {
                    if (File.Exists(ConfigFileInfo.CalibrationConfig) == false)
                    {
                        var cfg = new CalibrationCfgModel();
                        var content = Tool.ObjectToJson(cfg);
                        var bol = Tool.Wirte(ConfigFileInfo.CalibrationConfig, content);
                        if (bol == true)
                        {
                            _calibrationCfg = cfg;
                            Logger.Debug("相机配置文件不存在，创建默认配置文件");
                        }
                        else
                            Logger.Error("相机配置文件不存在，创建默认配置文件异常");
                    }
                    else
                    {
                        var content = Tool.Read(ConfigFileInfo.CalibrationConfig);
                        _calibrationCfg = Tool.JsonToObject<CalibrationCfgModel>(content);
                    }
                }

                return _calibrationCfg;
            }
        }
        private static CalibrationCfgModel _calibrationCfg;

        public static bool UpdateCalibrationCfg(CalibrationCfgModel cfg = null)
        {
            try
            {
                var content = Tool.ObjectToJson(cfg == null ? CalibrationCfg : cfg);
                var bol = Tool.Wirte(ConfigFileInfo.CalibrationConfig, content);
                if (bol == true)
                    _calibrationCfg = cfg;

                return bol;
            }
            catch (Exception ex)
            {
                Logger.Error("更新相机配置文件异常", ex);
                return false;
            }
        }
        #endregion
    }
}
